package com.ddz.common.result;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: LiQingBo
 * @Date: 2021/10/4-10-04-13:09
 * @Description: com.ddz.common.result
 * @version: 1.0
 */
@Data
public class BaseResult {
    private Integer code;
    private String message;
    private Map<String, Object> data = new HashMap<>();

    //构造函数私有化
    private BaseResult() {
    }

    /**
     * 返回成功的结果
     *
     * @return
     */
    public static BaseResult ok() {
        BaseResult baseResult = new BaseResult();
        baseResult.setCode(ResponseEnum.SUCCESS.getCode());
        baseResult.setMessage(ResponseEnum.SUCCESS.getMessage());
        return baseResult;
    }

    /**
     * 返回失败的结果
     *
     * @return
     */
    public static BaseResult error() {
        BaseResult baseResult = new BaseResult();
        baseResult.setCode(ResponseEnum.ERROR.getCode());
        baseResult.setMessage(ResponseEnum.ERROR.getMessage());
        return baseResult;
    }

    /**
     * 设置特定的结果
     *
     * @param result
     * @return
     */
    public static BaseResult setResult(ResponseEnum result) {
        BaseResult baseResult = new BaseResult();
        baseResult.setCode(result.getCode());
        baseResult.setMessage(result.getMessage());
        return baseResult;
    }

    public BaseResult data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public BaseResult data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

    /**
     * 设置特定的响应消息
     *
     * @param message
     * @return
     */
    public BaseResult message(String message) {
        this.setMessage(message);
        return this;
    }

    /**
     * 设置特定的响应码
     *
     * @param code
     * @return
     */
    public BaseResult code(Integer code) {
        this.setCode(code);
        return this;
    }

}
